'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _database = require('../config/database');

var _database2 = _interopRequireDefault(_database);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const getRebateStatusByOrderNo = (() => {
  var _ref = _asyncToGenerator(function* (orderNo) {
    let rebateInfo = null;
    let sql = `SELECT * FROM t_rebate
    WHERE order_no = ${orderNo}`;
    try {
      // console.log(sql)
      rebateInfo = yield _database2.default.query(sql);
      // console.log(rebateInfo)
    } catch (err) {
      console.log(`search t_rebate ${err}`);
    }
    return rebateInfo.length > 0 ? true : false;
  });

  return function getRebateStatusByOrderNo(_x) {
    return _ref.apply(this, arguments);
  };
})();

const createRebate = (() => {
  var _ref2 = _asyncToGenerator(function* (rebateInfo) {
    let rebatetkeys = Object.keys(rebateInfo);
    let rebatetvalues = [];
    for (let key in rebatetkeys) {
      rebatetvalues.push(rebateInfo[rebatetkeys[key]]);
    }
    let sql = `INSERT INTO t_rebate(${rebatetkeys})
    VALUES(${rebatetvalues})`;
    try {
      console.log(sql);
      yield _database2.default.query(sql);
      console.log('add t_rebate success');
    } catch (err) {
      console.log(`create t_rebate ${err}`);
    }
  });

  return function createRebate(_x2) {
    return _ref2.apply(this, arguments);
  };
})();

const updateRebateBySdcustomno = (() => {
  var _ref3 = _asyncToGenerator(function* (sdcustomno, distributor, todisbate) {
    let sql = `UPDATE t_rebate
    SET distributor=${distributor}, todisbate = ${todisbate} 
    WHERE order_no = ${sdcustomno}`;
    try {
      console.log(sql);
      yield _database2.default.query(sql);
      console.log('update t_rebate success');
    } catch (err) {
      console.log(`update t_rebate ${err}`);
    }
  });

  return function updateRebateBySdcustomno(_x3, _x4, _x5) {
    return _ref3.apply(this, arguments);
  };
})();

exports.default = {
  getRebateStatusByOrderNo,
  createRebate,
  updateRebateBySdcustomno
};